<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Guanghu
 *
 * @author dragon.hu add @2016.2.24
 */
require_once 'BaseChannel.php';

//广狐
class Guanghu implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="6a023ba6cb8613bff67fb990a3fe5f7d"; //APIKEY
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //秘钥
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("guanghu", $_REQUEST);
		
		$app_id = $_REQUEST['app_id'];  //appid
		$trade_no = $_REQUEST['trade_no'];  //狐 狸 系 统订单号
		$out_trade_no = $_REQUEST['out_trade_no'];  //游 戏 系 统自 己 的 订单号
		$total_fee = $_REQUEST['total_fee'];  //金额
		$time_end = $_REQUEST['time_end'];  //订单完成时间
		$buyer_id = $_REQUEST['buyer_id'];  //买 家 在 支付通道的用户名
		$huli_userid = $_REQUEST['huli_userid'];  //买家狐狸系统的用户id
		$pay_type = $_REQUEST['pay_type'];  //支付通道
		$pay_result = $_REQUEST['pay_result'];  //支付结果
		$extra = $_REQUEST['extra'];  //扩展参数
		$sign = $_REQUEST['sign'];  //签名
		$ver = $_REQUEST['ver'];  //版本号
		
		$arr = $_REQUEST;
		ksort($arr);
		//print_r($tmp);
		$tmp_str = '';
		foreach($arr as $k=>$v)
		{
			if(!in_array($k,array('sign')))//去除掉不参与签名的参数
			$tmp_str.=$k.$v;
		}
		$tmp_str.=$paymentKey;
		$tmp_str = strtolower(md5($tmp_str));
		
		if($sign == $tmp_str)  
		{ //验证通过
			$tmpstatus = STATUS_SUCCESS;
			if ($pay_result != 0)
			{
				$tmpstatus = STATUS_FAILD;
			}
			
			if(false == $orderLog->recordOrder($tmpstatus, $total_fee, $out_trade_no, "guanghu", $platform,$trade_no))
			{
				$ret="fail";
				$orderLog->log("guanghu",array("order"=>$out_trade_no,"money"=>$total_fee,"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$ret="success";
				$orderLog->log("guanghu",array("order"=>$out_trade_no,"money"=>$total_fee,"serverresult"=>"success"));
			}
			 
		} 
		else 
		{
			//"$sig验证失败"
			$orderLog->log("guanghu","  order:$out_trade_no  sign error sign:$sign mysign:$tmp_str");
			$ret="fail";
		}
		return $ret;
	}
	
}